﻿using JTE.Client.Services.ServiceConnect;
using JTE.Server.Models.Room;
using System;
using System.Linq;

namespace JTE_WCF_Client
{
    class Program
    {

        //cmd JTE_WCF_Client.exe admin 1 
        static void Main(string[] args)
        {
            //args = new string[] { "admin", "0123", "156", "1" };
            if (args == null || args.Length != 4)
            {
                Logger.Info(string.Format("刷新房态参数错误"));
                return;
            }
#if DEBUG
            Logger.Info(string.Format("刷新房态入参:{0} {1} {2} {3}", args));
#endif

            long id = Convert.ToInt64(args[2]);
            short lock_flag = Convert.ToInt16(args[3]);
            try
            {

                TRRoom rm = new TRRoom();
                rm.Id = id;
                var _room = RoomConnect.SearchRRoom(rm).FirstOrDefault();
                if (_room == null) return; 
                _room.LockRoom = new short?((short)lock_flag);
                RoomConnect.UpdateRRoom(_room);
            }
            catch (Exception ex)
            {
                Logger.Info(string.Format("刷新房态错误:{0}", ex));
            }

            //client.Close();
        }

        public static TReturn Invoke<TChannel, TReturn>(Func<TChannel, TReturn> func)
        {
            TReturn local = default(TReturn);
            TChannel arg = default(TChannel);
            try
            {
                arg = new System.ServiceModel.ChannelFactory<TChannel>(typeof(TChannel).Name).CreateChannel();
                local = func(arg);
                (arg as System.ServiceModel.ICommunicationObject).Close();
            }
            catch (System.ServiceModel.CommunicationException ex)
            {
                (arg as System.ServiceModel.ICommunicationObject).Abort();
                throw;
            }
            catch (TimeoutException ex)
            {
                (arg as System.ServiceModel.ICommunicationObject).Abort();
                throw;
            }
            catch (Exception ex)
            {
                (arg as System.ServiceModel.ICommunicationObject).Close();
                throw;
            }
            return local;
        }






    }
}
